package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.VehicleClassService;
import cn.huiyunche.base.service.mappers.BVehicleClassMapper;
import cn.huiyunche.base.service.model.BVehicleClass;
import cn.huiyunche.base.service.model.BVehicleClassExample;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class VehicleClassServiceImpl implements VehicleClassService {

    private static final Logger logger = LoggerFactory.getLogger(VehicleClassServiceImpl.class);

    @Autowired
    private BVehicleClassMapper vehicleClassMapper;

    @Override
    public Map<String, BigDecimal> getFactorRatio() {
        Map<String, BigDecimal> map = new HashMap<>();

        List<BVehicleClass> list = vehicleClassMapper.selectByExample(null);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取车型信息失败！");
        }
        BVehicleClass vehicleClass = null;
        for (int i = 0; i < list.size(); i++) {
            vehicleClass = list.get(i);
            map.put(vehicleClass.getVehicleClass(), vehicleClass.getFactorRatio());
        }
        return map;
    }

    @Override
    public BVehicleClass getByClass(String vehicleClass) {
        logger.info("getByClass param : {}", vehicleClass);
        if (StringUtils.isBlank(vehicleClass)) {
            logger.error("getByClass vehicleClass must not be bull");
            throw new BusinessException("车型不能为空！");
        }
        BVehicleClassExample example = new BVehicleClassExample();
        example.createCriteria().andVehicleClassEqualTo(vehicleClass);
        List<BVehicleClass> list = vehicleClassMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取车型信息失败！");
        }
        return list.get(0);
    }


}
